<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <h1>🐾 用户注册</h1>
        <p>加入我们，开启宠物生活新体验</p>
      </div>

      <el-form :model="registerForm" :rules="registerRules" ref="registerForm"
               class="register-form" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名（3-20个字符）"
            clearable>
          </el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码（6-20个字符）"
            show-password>
          </el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            show-password>
          </el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="registerForm.email"
            placeholder="请输入邮箱地址"
            clearable>
          </el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="registerForm.phone"
            placeholder="请输入手机号码"
            clearable>
            <el-button slot="append" @click="handleSendCode" :disabled="countdown > 0">
              {{ countdown > 0 ? `${countdown}秒后重试` : '获取验证码' }}
            </el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="验证码" prop="code">
          <el-input
            v-model="registerForm.code"
            placeholder="请输入验证码"
            clearable>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="registerForm.agree">
            我已阅读并同意
            <el-button type="text">《用户协议》</el-button>
            和
            <el-button type="text">《隐私政策》</el-button>
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleRegister"
                     style="width: 100%" size="large">
            注册
          </el-button>
        </el-form-item>

        <el-form-item>
          <div class="login-link">
            已有账号？
            <el-button type="text" @click="goToLogin">立即登录</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data () {
    // 确认密码验证
    const validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }

    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        email: '',
        phone: '',
        code: '',
        agree: false
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_]+$/, message: '只能包含字母、数字和下划线', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, validator: validateConfirmPassword, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      loading: false,
      countdown: 0
    }
  },
  methods: {
    handleSendCode () {
      if (!this.registerForm.phone) {
        this.$message.warning('请先输入手机号码')
        return
      }
      if (!/^1[3-9]\d{9}$/.test(this.registerForm.phone)) {
        this.$message.warning('请输入正确的手机号码')
        return
      }

      // 模拟发送验证码
      this.countdown = 60
      this.$message.success('验证码已发送')

      const timer = setInterval(() => {
        this.countdown--
        if (this.countdown <= 0) {
          clearInterval(timer)
        }
      }, 1000)
    },
    handleRegister () {
      if (!this.registerForm.agree) {
        this.$message.warning('请先阅读并同意用户协议和隐私政策')
        return
      }

      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.loading = true
          // 模拟注册 - 将来对接接口 POST /api/user/register
          setTimeout(() => {
            this.loading = false
            this.$message.success('注册成功！')
            // 跳转到登录页
            this.$router.push('/login')
          }, 1500)
        }
      })
    },
    goToLogin () {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px 20px;
}

.register-box {
  width: 600px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}

.register-header {
  text-align: center;
  padding: 40px 40px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.register-header h1 {
  font-size: 32px;
  margin: 0 0 10px 0;
}

.register-header p {
  font-size: 16px;
  margin: 0;
  opacity: 0.9;
}

.register-form {
  padding: 40px 50px;
}

.login-link {
  text-align: center;
  color: #666;
}
</style>
